package com.asm.controller.api;

import com.asm.controller.BaseController;
import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.ContractService;
import com.asm.vo.sys.ContractVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


/**
 * @author tanzujie
 */
@RestController
public class ContractController extends BaseController {
    @Resource
    private ContractService contractService;

    /**
     * 分页查询所有合同
     *
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 合同
     */
    @ApiOperation("接口描述:分页查询所有合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小")
    })
    @GetMapping("/contracts")
    public ResponseEntity<PageResult<ContractVo>> getAllContracts(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", contractService.findAllContracts(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 查询所有合同信息
     *
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 合同信息
     */
    @ApiOperation("接口描述:查询所有合同信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小")
    })
    @GetMapping("/contractMsgs")
    public ResponseEntity<PageResult<Map<String, Object>>> getAllContractMsgs(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", contractService.findAllContractMsgs(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据业务员编号查询合同信息
     *
     * @param sellId     业务员编号
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 合同信息
     */
    @ApiOperation("接口描述:根据业务员编号查询合同信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sellId", value = "业务员编号"),
            @ApiImplicitParam(name = "pageNumber", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小")
    })
    @GetMapping("/contractMsgs/sellId/{sellId}")
    public ResponseEntity<PageResult<Map<String, Object>>> getContractMsgsBySellId(@PathVariable("sellId") Long sellId, @RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", contractService.findContractMsgsBySellId(sellId, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据顾客编号查询合同信息
     *
     * @param customerId 顾客编号
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 合同信息
     */
    @ApiOperation("接口描述:根据顾客编号查询合同信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "顾客编号"),
            @ApiImplicitParam(name = "pageNumber", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小")
    })
    @GetMapping("/contractMsgs/customerId/{customerId}")
    public ResponseEntity<PageResult<Map<String, Object>>> getContractMsgsByCustomerId(@PathVariable("customerId") Long customerId, @RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", contractService.findContractMsgsByCustomerId(customerId, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据Id获取合同
     *
     * @param id 合同编号
     * @return 合同
     */
    @ApiOperation("接口描述:根据Id获取合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同编号")
    })
    @GetMapping("/contract/{id}")
    public ResponseEntity<Result<ContractVo>> getContractById(@PathVariable("id") Long id) {
        return new ResponseEntity(new Result().ok("success", contractService.findContractById(id)), HttpStatus.OK);
    }

    /**
     * 根据合同名字分页查询合同
     *
     * @param name       合同名字
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 合同
     */
    @ApiOperation("接口描述:根据合同名字分页查询合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "合同名字"),
            @ApiImplicitParam(name = "pageNumber", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @GetMapping("/contracts/name/{name}")
    public ResponseEntity<PageResult<ContractVo>> getContractByContractName(@PathVariable("name") String name, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", contractService.findContractByContractName(name, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 增加合同
     *
     * @param contractVo 合同
     * @return 操作状态
     */
    @ApiOperation("接口描述:增加合同")
    @PostMapping("/contract/add")
    public ResponseEntity<Result> addContract(@RequestBody @ApiParam(name = "contractVo", value = "合同") ContractVo contractVo) {
        contractService.addContract(contractVo);
        return new ResponseEntity(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 更新合同
     *
     * @param contractVo 合同对象
     * @return 操作状态
     */
    @ApiOperation("接口描述:更新合同")
    @PutMapping("/contract/update")
    public ResponseEntity<Result> updateContract(@RequestBody @ApiParam(name = "contractVo", value = "合同") ContractVo contractVo) {
        contractService.updateContract(contractVo);
        return new ResponseEntity(new Result().ok("update success"), HttpStatus.OK);
    }

    /**
     * 根据Id删除合同
     *
     * @param id 合同编号
     * @return 操作状态
     */
    @ApiOperation("接口描述:根据Id删除合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同编号")
    })
    @DeleteMapping("/contract/delete/{id}")
    public ResponseEntity<Result> deleteContract(@PathVariable("id") Long id) {
        contractService.deleteContract(id);
        return new ResponseEntity(new Result().ok("delete success"), HttpStatus.OK);
    }

}
